package de.geckocraft.GeckoEventBroadcaster.Commands;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import de.geckocraft.GeckoEventBroadcaster.Main.Main;
import de.geckocraft.GeckoEventBroadcaster.Main.MainConfig;
import de.geckocraft.GeckoEventBroadcaster.Main.Template;
import de.geckocraft.GeckoEventBroadcaster.Main.EventList;

public class Events
		implements CommandExecutor
{

	@SuppressWarnings("unused")
	private Main	plugin;

	public Events(Main plugin)
	{
		this.plugin = plugin;
	}

	public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args)
	{
		if (!(sender instanceof Player))
		{
			sender.sendMessage(ChatColor.RED + "Sie sind kein Spieler!");
			return true;
		}
		Player p = (Player) sender;
		if (cmd.getName().equalsIgnoreCase("event"))
		{
			if (args.length > 0)
			{
				if (args[0].equalsIgnoreCase("list"))
				{
					if (p.hasPermission("gecko.eventBroadcaster.listadmin") || p.hasPermission("gecko.eventBroadcaster.list"))
					{
						eventList(p);
					}
					else
					{
						Template.sendMessage("nopermission", p);
					}
				}
				if (args[0].equalsIgnoreCase("add"))
				{
					if (p.hasPermission("gecko.eventBroadcaster.add"))
					{
						eventAdd(p, args);
					}
					else
					{
						Template.sendMessage("nopermission", p);
					}
				}
				if (args[0].equalsIgnoreCase("remove"))
				{
					if (p.hasPermission("gecko.eventBroadcaster.remove"))
					{
						eventRemove(p, args);
					}
					else
					{
						Template.sendMessage("nopermission", p);
					}
				}
				if ((args[0].equalsIgnoreCase("reload")))
				{
					if (p.hasPermission("gecko.eventBroadcaster.reload"))
					{
						Main.loadConfigs();
						Template.sendMessage("reload", p);
					}
					else
					{
						Template.sendMessage("nopermission", p);
					}
				}
			}
			else
			{
				if (p.hasPermission("gecko.eventBroadcaster.listadmin") || p.hasPermission("gecko.eventBroadcaster.list"))
				{
					showNextEvent(p);
				}
				else
				{
					Template.sendMessage("nopermission", p);
				}
			}

		}
		return false;
	}

	private void eventRemove(Player p, String[] args)
	{
		if (Main.isInteger(args[1]))
		{
			try
			{
				EventList.removeEvent(Integer.parseInt(args[1]));
				Template.sendMessage("removed", p);

			} catch (Exception e)
			{
			}
		}
		else
		{
			Template.sendMessage("nonum", p, args[1]);
		}
	}

	@SuppressWarnings("deprecation")
	private void showNextEvent(Player p)
	{
		if (!EventList.Eventdate.isEmpty())
		{
			Date eventdate = EventList.Eventdate.get(EventList.Eventnummbers.get(0));
			String eventname = EventList.Eventname.get(EventList.Eventnummbers.get(0));
			Date now = new Date();
			long diferensSek = (eventdate.getTime() - now.getTime()) / 1000;
			long stundenSek = eventdate.getHours() * 60 * 60;
			long minutenSek = eventdate.getMinutes() * 60;
			long diferensDaySek = diferensSek - stundenSek - minutenSek;

			if (diferensDaySek < 0)
			{
				if (diferensSek * -1 > MainConfig.getchangetext())
				{
					String time = "" + eventdate.getHours() + ":" + eventdate.getMinutes();
					Template.sendMessage("today", p, time, eventname);
				}
				else
				{

					long stundenchach = (diferensSek + 1) / 60 / 60;
					long minutenchach = (diferensSek + 1) / 60;
					long diferensDaySekchach = (diferensSek + 1);

					while (diferensDaySekchach >= (1 * 60 * 60))
					{
						diferensDaySekchach = diferensDaySekchach - 60 * 60;
						minutenchach = diferensDaySekchach / 60;
					}

					String stunden = "" + stundenchach;
					String min = "" + minutenchach;

					if (minutenchach == 0)
					{

						Template.sendMessage("todayh", p, stunden, eventname);
					}
					else if (stundenchach == 0)
					{

						Template.sendMessage("todaymin", p, min, eventname);
					}
					else
					{
						Template.sendMessage("todayhmin", p, stunden, min, eventname);
					}
				}
			}
			else if (diferensDaySek < 86400)
			{
				String time = "" + eventdate.getHours() + ":" + eventdate.getMinutes();
				Template.sendMessage("today", p, time, eventname);
			}
			else
			{
				String datum = "" + eventdate.getDay() + "." + eventdate.getMonth() + "." + eventdate.getYear();
				String time = "" + eventdate.getHours() + ":" + eventdate.getMinutes();
				Template.sendMessage("longer", p, datum, time, eventname);
			}
		}
		else
		{
			Template.sendMessage("noevent", p);
		}
	}

	private void eventList(Player p)
	{

		HashMap<Integer, String> Eventname = EventList.getEventname();
		HashMap<Integer, Date> Eventdate = EventList.getEventdate();
		ArrayList<Integer> Eventnummbers = EventList.getEventnummbers();

		if (!Eventname.isEmpty())
		{
			for (int j = 0; Eventnummbers.size() > j; j++)
			{

				Integer eventnummer = Eventnummbers.get(j);
				Date datum = Eventdate.get(eventnummer);
				String eventname = Eventname.get(eventnummer);
				SimpleDateFormat formater = new SimpleDateFormat();
				if (p.hasPermission("gecko.eventBroadcaster.listadmin"))
				{

					Template.sendMessage("atadmin", p, eventnummer.toString(), formater.format(datum).toString(), eventname);
				}
				else
				{
					Template.sendMessage("at", p, formater.format(datum).toString(), eventname);
				}
			}
		}
		else
		{
			Template.sendMessage("noevent", p);
		}
	}

	private void eventAdd(Player p, String[] args)
	{
		if (p.hasPermission("gecko.EventBroadcaster.add"))
		{
			if (args.length >= 4)
			{
				if (isDate(args[args.length - 2]))
				{
					if (isTime(args[args.length - 1]))
					{
						String name = "";
						for (int i = 1; i < args.length - 2; i++)
						{
							if (name.equals(""))
							{
								name = args[1];
							}
							else
							{
								name = name + " " + args[i];
							}
						}
						EventList.addEvents(name, args[args.length - 2], args[args.length - 1]);
						Template.sendMessage("added", p, name);
					}
					else
					{
						Template.sendMessage("notime", p, args[args.length - 1]);
						Template.sendMessage("commandadd", p);
					}
				}
				else
				{
					Template.sendMessage("nodate", p, args[args.length - 2]);
					Template.sendMessage("commandadd", p);
				}
			}
			else
			{
				Template.sendMessage("commandadd", p);
			}
		}
		else
		{
			Template.sendMessage("nopermission", p);
		}
	}

	private boolean isDate(String s)
	{
		SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
		@SuppressWarnings("unused")
		Date d = null;

		try
		{
			d = format.parse(s);

		} catch (ParseException ex)
		{
			return false;
		}
		return true;
	}

	private boolean isTime(String s)
	{
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		@SuppressWarnings("unused")
		Date d = null;

		try
		{
			d = format.parse(s);

		} catch (ParseException ex)
		{
			return false;
		}
		return true;
	}

}
